home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / dodge.swf / scripts / __Packages / Enemy.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  4.9 KB  |  192 lines

  1. class Enemy extends MovieClipHolder implements Steppable
  2. {
  3.    var mc;
  4.    var health;
  5.    var targetCoords;
  6.    var xspeed;
  7.    var yspeed;
  8.    var tm;
  9.    var dm;
  10.    static var SCORE = 100;
  11.    static var SPEED = 5;
  12.    static var WAIT_TIME = 60;
  13.    static var ENEMY_TYPE = "enemy1";
  14.    static var MAX_HEALTH = 2;
  15.    static var COLOR = 16776960;
  16.    static var SHAPE_FLAG = false;
  17.    static var enemies = new ObjectList();
  18.    var timer = 0;
  19.    var missileCheckNumber = 0;
  20.    function Enemy(x, y)
  21.    {
  22.       super(_root.attachMovie(this.getEnemyType(),this.getEnemyType() + _root.getNextHighestDepth(),_root.getNextHighestDepth()),this.getColor(),true);
  23.       this.mc._x = x;
  24.       this.mc._y = y;
  25.       this.assignNewTarget();
  26.       this.createTrailManager();
  27.       this.createDebrisManager();
  28.       this.health = this.getMaxHealth();
  29.       Stepper.add(this);
  30.       Enemy.add(this);
  31.       var _loc4_ = Math.atan2(this.targetCoords.y - this.mc._y,this.targetCoords.x - this.mc._x);
  32.       this.xspeed = this.getSpeed() * Math.cos(_loc4_);
  33.       this.yspeed = this.getSpeed() * Math.sin(_loc4_);
  34.       this.mc._rotation = CustomMath.radToDeg(_loc4_);
  35.    }
  36.    function getScore()
  37.    {
  38.       return Enemy.SCORE;
  39.    }
  40.    function getSpeed()
  41.    {
  42.       return Enemy.SPEED;
  43.    }
  44.    function getEnemyType()
  45.    {
  46.       return Enemy.ENEMY_TYPE;
  47.    }
  48.    function getWaitTime()
  49.    {
  50.       return Enemy.WAIT_TIME;
  51.    }
  52.    function getMaxHealth()
  53.    {
  54.       return Enemy.MAX_HEALTH;
  55.    }
  56.    function getColor()
  57.    {
  58.       return Enemy.COLOR;
  59.    }
  60.    function getShapeFlag()
  61.    {
  62.       return Enemy.SHAPE_FLAG;
  63.    }
  64.    static function add(m)
  65.    {
  66.       Enemy.enemies.add(m);
  67.    }
  68.    static function remove(m)
  69.    {
  70.       Enemy.enemies.remove(m);
  71.    }
  72.    function step()
  73.    {
  74.       if(this.checkProximity())
  75.       {
  76.          this.shootIfAble();
  77.          this.tm.die();
  78.          delete this.tm;
  79.       }
  80.       else
  81.       {
  82.          this.move();
  83.       }
  84.       this.checkHit();
  85.    }
  86.    function createDebrisManager()
  87.    {
  88.       this.dm = new DebrisManager(this.getColor(),6);
  89.    }
  90.    function createTrailManager()
  91.    {
  92.       if(_root.effects >= 2)
  93.       {
  94.          this.tm = new TrailManager(this.mc,this.mc.exhaust1,1,16566798);
  95.       }
  96.       else
  97.       {
  98.          this.tm = new TrailManager(this.mc,this.mc.exhaust1,1);
  99.       }
  100.    }
  101.    function checkProximity()
  102.    {
  103.       return Math.abs(this.mc._x - this.targetCoords.x) < 10 && Math.abs(this.mc._y - this.targetCoords.y) < 10;
  104.    }
  105.    function move()
  106.    {
  107.       this.mc._x += this.xspeed;
  108.       this.mc._y += this.yspeed;
  109.       this.tm.makeTrail();
  110.    }
  111.    function getWeaponsPoints()
  112.    {
  113.       var _loc2_ = new flash.geom.Point(this.mc.weapon1._x,this.mc.weapon1._y);
  114.       this.mc.localToGlobal(_loc2_);
  115.       return new Array(_loc2_);
  116.    }
  117.    function shootIfAble()
  118.    {
  119.       this.rotateToPlayer();
  120.       if(this.timer > this.getWaitTime())
  121.       {
  122.          var _loc4_ = this.getWeaponsPoints();
  123.          var _loc2_ = 0;
  124.          while(_loc2_ < _loc4_.length)
  125.          {
  126.             var _loc3_ = _loc4_[_loc2_];
  127.             this.createNewMissile(_loc3_.x,_loc3_.y);
  128.             _loc2_ = _loc2_ + 1;
  129.          }
  130.          this.timer = 0;
  131.       }
  132.       this.timer = this.timer + 1;
  133.    }
  134.    function assignNewTarget()
  135.    {
  136.       this.targetCoords = new flash.geom.Point(CustomMath.randomRange(100,Stage.width - 100),CustomMath.randomRange(100,Stage.height - 100));
  137.    }
  138.    function createNewMissile(x, y)
  139.    {
  140.       new Missile(x,y);
  141.    }
  142.    function rotateToPlayer()
  143.    {
  144.       var _loc3_ = Math.atan2(_root.player.getMovie()._y - this.mc._y,_root.player.getMovie()._x - this.mc._x);
  145.       this.mc._rotation = CustomMath.radToDeg(_loc3_);
  146.    }
  147.    function checkHit()
  148.    {
  149.       var _loc3_ = this.missileCheckNumber;
  150.       while(_loc3_ < Missile.missiles.getLength())
  151.       {
  152.          var _loc2_ = Missile.missiles["get"](_loc3_);
  153.          if(_loc2_.hit(this.mc,this.getShapeFlag()))
  154.          {
  155.             this.health -= _loc2_.getDamage();
  156.             _loc2_.die();
  157.          }
  158.          _loc3_ += 2;
  159.       }
  160.       if(this.health <= 0)
  161.       {
  162.          this.die();
  163.       }
  164.       if(this.missileCheckNumber == 0)
  165.       {
  166.          this.missileCheckNumber = 1;
  167.       }
  168.       else
  169.       {
  170.          this.missileCheckNumber = 0;
  171.       }
  172.    }
  173.    function die()
  174.    {
  175.       if(this.health <= 0)
  176.       {
  177.          ScoreManager.addScore(this.getScore());
  178.          ScoreManager.incrementMultiplier();
  179.       }
  180.       this.tm.die();
  181.       this.dm.createDebrisField(this.mc._x,this.mc._y);
  182.       Stepper.remove(this);
  183.       Enemy.remove(this);
  184.       this.mc.blendMode = "normal";
  185.       var _loc3_ = new Array();
  186.       _loc3_.push(new flash.filters.GlowFilter(this.getColor(),100,15,15,1.5,1));
  187.       this.mc.filters = _loc3_;
  188.       this.mc.swapDepths(_root.getNextHighetDepth());
  189.       this.mc.gotoAndPlay("death");
  190.    }
  191. }
  192.